// pages/classify/classify.js
import { request } from "../../request/request.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {

    classification:[{id:0,img:"../../images/classify/circle.png",text:"可回收垃圾"},{id:1,img:"../../images/classify/fruit.png",text:"厨余垃圾"},{id:2,img:"../../images/classify/bad.png",text:"有害垃圾"},{id:3,img:"../../images/classify/other.png",text:"其他垃圾"}],
    select: 0,
    rubbish: [],

  },
  changeIndex(e) {
    let that=this
    // console.log()
    this.setData({
      select: e.currentTarget.dataset.index
    })
    wx.cloud.callFunction({
      name: 'findGarbage',
      data:{
classify:that.data.classification[that.data.select].text
      },
      success: function(res) {
// console.log(res.result.data)
that.setData({
  rubbish:res.result.data
})

      }})
    // request('get', 'https://nantuteng.co/findDataByRubbishClass', this.data.classification[this.data.select].text).then(res => {
    //   this.setData({
    //     rubbish: res.data
    //   })
    // })
   
  },

  click: function (options) {
    
    let that = this;
    let result = this.data.rubbish[options.currentTarget.dataset.id];
    // console.log(result);
    wx.navigateTo({
      url: `/pages/search/search?type=${result.rubbish_name}`
    })
  
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log("建立")
    let that=this
    wx.cloud.callFunction({
      name: 'findGarbage',
      data:{
classify:that.data.classification[that.data.select].text
      },
      success: function(res) {
// console.log(res)
that.setData({
  rubbish:res.result.data
})
      }})
    // request('get', 'https://nantuteng.co/findDataByRubbishClass', this.data.classification[this.data.select].text).then(res => {
    //   console.log(res)
    //   // return request()
    //   this.setData({
    //     rubbish: res.data
    //   })
    //   // console.log(this.data.rubbish)
    // }).catch(e=>{
    //   console.log(e)
    // })
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})